// utils/sse.js
export const createSSEConnection = (url, onMessage, onError, onComplete) => {
    const eventSource = new EventSource(url);

    eventSource.onmessage = (event) => {
        // const data = JSON.parse(event.data); // 假设服务器返回的是 JSON 数据

        onMessage(event.data);
    };

    eventSource.onerror = (error) => {
        onError(error);
        eventSource.close(); // 关闭连接
        onComplete(false); // 标记为完成
    };

    return eventSource;
};